package cultu.customer.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import cultu.core.message.Context;
import cultu.customer.dao.CustomerDAO;
import cultu.customer.message.Customer;
import cultu.customer.message.CustomerCriteria;
import cultu.customer.message.CustomerList;
import cultu.customer.message.CustomerScenario;
import cultu.customer.service.CustomerManager;

/**
 * Customer service implementation.
 * @author Marc-Antoine
 *
 */
@Transactional
public class CustomerManagerImpl implements CustomerManager {

  /**
   * Customer Dao.
   */
  private CustomerDAO customerDao;

  /**
   * Sets customer Dao.
   * @param dao the dao to set.
   */
  public void setCustomerDao(final CustomerDAO dao){
    this.customerDao = dao;
  }

  /**
   * {@inheritDoc}
   */
  @Transactional(readOnly = true)
  public Customer getCustomer(final Context pContext, final CustomerScenario pScenario, final Customer pKey) {

    Customer result = null;

    // Check object.
    if (validateCustomer(pKey)) {
      result = customerDao.get(pKey, pScenario);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public Customer createCustomer (final Context pContext, final CustomerScenario pScenario, final Customer pCustomer) {

    Customer result = null;

    // Check.
    if (validateCustomer(pCustomer)) {
      result = customerDao.create(pCustomer);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public Customer deleteCustomer(final Context pContext, final CustomerScenario pScenario, final Customer pKey) {
    Customer result =null;

    // Check.
    if(validateCustomer(pKey)) {
      result = customerDao.delete(pKey);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public Customer updateCustomer(final Context pContext, final CustomerScenario pScenario, final Customer pCustomer) {

    Customer result =null;

    // Check.
    if (validateCustomer(pCustomer)) {
      result = customerDao.update(pCustomer);
    }
    return result;
  }

  /**
   * {@inheritDoc}
   */
  public CustomerList findCustomers(final Context pContext, final CustomerScenario pScenario, final CustomerCriteria pCriteria) {

    CustomerList result = new CustomerList();

    if (pCriteria != null) {
      List<Customer> myList = customerDao.find(pCriteria, pScenario);
      result.getCustomers().addAll(myList);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public CustomerList deleteCustomers(final Context pContext, final CustomerScenario pScenario, final CustomerList pList) {

    CustomerList result = new CustomerList();

    if (pList != null) {
      for (Customer myUser : pList.getCustomers()) {
        result.getCustomers().add(deleteCustomer(pContext, pScenario, myUser));
      }
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public CustomerList createCustomers(final Context pContext, final CustomerScenario pScenario, final CustomerList pList) {

    CustomerList result = new CustomerList();

    if (pList != null) {
      for (Customer myUser : pList.getCustomers()) {
        result.getCustomers().add(createCustomer(pContext, pScenario,  myUser));
      }
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public CustomerList updateCustomers(final Context pContext, final CustomerScenario pScenario, final CustomerList pList) {

    CustomerList result = new CustomerList();

    if (pList != null) {
      for (Customer myUser : pList.getCustomers()) {
        result.getCustomers().add(updateCustomer(pContext, pScenario,  myUser));
      }
    }

    return result;
  }

  /**
   * Check that given object has enough data to be used.
   *
   * @param customer th customer to check.
   * @return true, if successful
   */
  private boolean validateCustomer (final Customer customer) {

    // pas de controle sur receipt
    return customer != null && (!"".equals(customer.getLastname()) || customer.getHjid() != null);
  }

  /**
   * increaseScore
   * @param customerId
   * @param communitiesId
   * @param increase
   */
  public void increaseScore(final Long customerId, List<Long> communitiesId, final Integer increase) {
    customerDao.increaseScore(customerId, communitiesId, increase);
  }
}
